﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Images;
using System.Drawing;
using Silvermoon.Controls;
using Silvermoon.OpenGL;
using Silvermoon.Core;
using Silvermoon.Utils;
using Silvermoon.Windows;
using Silvermoon.OpenGL.Native;

namespace Silvermoon.Shapes
{

    public class TrackbarButtonShape : ColorShape
    {
        //protected override Texture TextureCore { get { return SysImages.RoundButton; } }
        protected override Texture TextureCore { get { return SysImages.TrackbarButton; } }
        protected override Color GetDefaultColor() { return Color.DarkGray; }
        protected override RenderStyle StyleCore { get { return RenderStyle.Edged; } }

        protected override Shape CreateClone()
        {
            return new TrackbarButtonShape();
        }
    }


    /// <summary>
    /// Renders a shape with a resolution of 5551.
    /// See also <seealso cref="HighQualityButtonShape"/>.
    /// </summary>
    public class ButtonShape : ColorShape
    {
        protected override Color GetDefaultColor() { return Color.FromArgb(24 << 16 | 24 << 8 | 24); }
        protected override RenderStyle StyleCore { get { return RenderStyle.Edged; } }

        protected override Texture TextureCore { get { return SysImages.Accent; } }
        //protected override Texture TextureCore { get { return SysImages.AccentLuminance; } }
        //protected override Texture AlphaTextureCore { get { return SysImages.AlphaAccent; } }

        protected override Shape CreateClone()
        {
            return new ButtonShape();
        }

    }

    /// <summary>
    /// Same as ButtonShape, but uses two textures, one for alpha and one for luminance for higher resolution of 8888 instead of 5551.
    /// </summary>
    public class HighQualityButtonShape : ColorShape
    {
        protected override Color GetDefaultColor() { return Color.FromArgb(24 << 16 | 24 << 8 | 24); }
        protected override RenderStyle StyleCore { get { return RenderStyle.Edged; } }

        //        protected override Texture TextureCore { get { return SysImages.Accent; } }
        protected override Texture TextureCore { get { return SysImages.AccentLuminance; } }
        protected override Texture AlphaTextureCore { get { return SysImages.AlphaAccent; } }

        protected override Shape CreateClone()
        {
            return new HighQualityButtonShape();
        }
    }


    public class PlasticButtonShape : ButtonShape
    {
        protected override Texture TextureCore { get { return SysImages.Button; } }
        protected override RenderStyle StyleCore { get { return RenderStyle.Plastic; } }

        protected override Shape CreateClone()
        {
            return new PlasticButtonShape();
        }
    }

    public class MetalicButtonShape : ButtonShape
    {
        protected override Texture TextureCore { get { return SysImages.Button; } }
        protected override RenderStyle StyleCore { get { return RenderStyle.Metalic; } }

        protected override Shape CreateClone()
        {
            return new MetalicButtonShape();
        }
    }

    public class ProgressBarShape : ColorShape
    {
        //protected override Texture TextureCore { get { return SysImages.Button; } }
        protected override Texture TextureCore { get { return SysImages.ProgressBorder; } }
        protected override Color GetDefaultColor() { return Color.Silver; }
        protected override RenderStyle StyleCore { get { return RenderStyle.Metalic; } }

        protected override Shape CreateClone()
        {
            return new ProgressBarShape();
        }
    }

    public class RoundedEdgeShape : ColorShape
    {
        //protected override Texture TextureCore { get { return SysImages.Button; } }
        protected override Texture TextureCore { get { return SysImages.Rounded; } }
        protected override Color GetDefaultColor() { return Color.White; }
        protected override RenderStyle StyleCore { get { return RenderStyle.Edged; } }

        protected override Shape CreateClone()
        {
            return new RoundedEdgeShape();
        }
    }

    public class TrackbarShape : ColorShape
    {
        //protected override Texture TextureCore { get { return SysImages.Button; } }
        protected override Texture TextureCore { get { return SysImages.TrackbarRange; } }
        protected override Color GetDefaultColor() { return Color.Silver; }
        protected override RenderStyle StyleCore { get { return RenderStyle.Edged; } }

        protected override Shape CreateClone()
        {
            return new TrackbarShape();
        }
    }

    public class TrackbarFillShape : ColorShape
    {
        protected override Texture TextureCore { get { return SysImages.TrackbarFill; } }
        protected override Color GetDefaultColor() { return Color.Silver; }
        protected override RenderStyle StyleCore { get { return RenderStyle.Edged; } }

        protected override Shape CreateClone()
        {
            return new TrackbarFillShape();
        }
    }

    public class GrabberShape : BackgroundShape
    {
        protected override Texture TextureCore { get { return SysImages.Grabber; } }
        protected override Color GetDefaultColor() { return PhoneColors.Blue; }

        public GrabberShape()
            : base()
        {
            Size = new Size(18, 18);
            this.Transformations = new Transformation[] { UnclippedTransform.Instance };
        }

        protected override Shape CreateClone()
        {
            return new GrabberShape();
        }
    }

    public class RectBorderFillShape : MultiShape
    {
        private BorderShape borderShape;
        public RectBorderFillShape()
            : base()
        {
            Shapes.Add(Color.Transparent);
            borderShape = new BorderShape(3, PhoneColors.Text);
            Shapes.Add(borderShape);
        }

        public int BorderSize
        {
            get { return borderShape.BorderSize; }
            set
            {
                if (borderShape.BorderSize != value)
                {
                    borderShape.BorderSize = value;
                    Invalidate();
                }
            }
        }

        public BorderEdge Edges
        {
            get { return borderShape.Edges; }
            set
            {
                if (borderShape.Edges != value)
                {
                    borderShape.Edges = value;
                    Invalidate();
                }
            }
        }

        public Color BorderColor
        {
            get { return borderShape.Color; }
            set
            {
                if (borderShape.Color != value)
                {
                    borderShape.Color = value;
                    Invalidate();
                }
            }
        }

        protected override Shape CreateClone()
        {
            return new RectBorderFillShape();
        }

        protected override void Copy(Shape shape)
        {
            base.Copy(shape);
            RectBorderFillShape s = shape as RectBorderFillShape;
            s.BorderColor = BorderColor;
        }
    }

    public class InnerOuterBorderShape : MultiShape
    {
        private BorderShape innerShape;
        private BorderShape outerShape;

        public InnerOuterBorderShape()
            : base()
        {
            outerShape = new BorderShape(3, PhoneColors.Text);
            innerShape = new BorderShape(3, PhoneColors.Background);
            Shapes.Add(outerShape);
            Shapes.Add(innerShape);
        }

        protected override void OnLayout()
        {
            Rectangle bounds = new Rectangle(0, 0, Width, Height);
            outerShape.Bounds = bounds;
            bounds.Inflate(outerShape.BorderSize, outerShape.BorderSize);
            innerShape.Bounds = bounds;
        }
    }


    public class ContextMenuShape : ShadowedBackgroundShape
    {
        protected override Texture TextureCore { get { return SysImages.Rounded; } }
        protected override RenderStyle StyleCore { get { return RenderStyle.Gradiented; } }
        protected override Color GetDefaultColor() { return Color.LightGray; }

        protected override Shape CreateClone()
        {
            return new ContextMenuShape();
        }
    }

    public class TileShape : BackgroundShape
    {
        protected override Texture TextureCore { get { return SysImages.Rounded; } }
        protected override RenderStyle StyleCore { get { return RenderStyle.Gradiented; } }
        protected override Color GetDefaultColor() { return Color.LightGray; }

        protected override Shape CreateClone()
        {
            return new TileShape();
        }
    }


    public class TextBoxShape : ColorShape
    {
        protected override Texture TextureCore { get { return SysImages.TextBox; } }
        protected override RenderStyle StyleCore { get { return RenderStyle.Edged; } }
        protected override Color GetDefaultColor() { return Color.White; }

        protected override Shape CreateClone()
        {
            return new TextBoxShape();
        }
    }

    public class ModelContextMenuShape : BorderFillShape
    {

        public ModelContextMenuShape()
            : base()
        {
            Edges = BorderEdge.Top | BorderEdge.Bottom;
            BorderSize = 3;
            BorderColor = PhoneColors.MediumDark;
            Color = PhoneColors.White;
        }

        protected override Shape CreateClone()
        {
            return new ModelContextMenuShape();
        }
    }

    public class LenseShape : MultiShape
    {
        private GradientShape gradientShape;
        private BorderShape borderShape;

        private Color color = ColorUtil.FromArgb(255, 119, 212, 255);
        public LenseShape()
            : base()
        {
            gradientShape = new GradientShape { EndColor = ColorUtil.FromArgb(32, 119, 212, 255), StartColor = ColorUtil.FromArgb(128, 119, 212, 255) };
            //gradientShape = new BackgroundShape { Color = ColorUtil.AlphaColor(color, 128), Style = RenderStyle.Metalic };
            borderShape = new BorderShape(4, color);
            Shapes.Add(gradientShape);
            Shapes.Add(borderShape);
        }

        public override Color Color
        {
            get
            {
                return color;
            }
            set
            {
                if (color != null)
                {
                    color = value;
                    OnColorChanged();
                }
            }
        }

        private void OnColorChanged()
        {
            //gradientShape.Color = ColorUtil.AlphaColor(color, 128);
            gradientShape.EndColor = ColorUtil.AlphaColor(color, 32);
            gradientShape.StartColor = ColorUtil.AlphaColor(color, 128);
            borderShape.Color = color;
            gradientShape.Invalidate();
            borderShape.Invalidate();
            Invalidate();
        }

        protected override Shape CreateClone()
        {
            return new LenseShape();
        }

    }



    //public class RectBorderShape : BackgroundShape
    //{
    //    protected override Texture TextureCore { get { return SysImages.RectBorder; } }
    //    protected override RenderStyle StyleCore { get { return RenderStyle.Edged; } }
    //    protected override Color GetDefaultColor() { return Color.White; }
    //}


    public class SelectedItemShape : BackgroundShapeBase
    {
        protected override Texture TextureCore { get { return SysImages.ItemBackground; } }
        protected override Color ColorCore { get { return SysColors.HighlightedItemColor; } }
        protected override RenderStyle StyleCore { get { return RenderStyle.FlatChrome; } }

        protected override Shape CreateClone()
        {
            return new SelectedItemShape();
        }
    }
}
